﻿package cn.poco.pepsi.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;
import cn.poco.pepsi.bean.Area;
import cn.poco.pepsi.bean.Circle;
import cn.poco.pepsi.bean.Dish;
import cn.poco.pepsi.bean.HotSearchKey;
import cn.poco.pepsi.bean.ResDetail;
import cn.poco.pepsi.bean.Restaurant;
import cn.poco.pepsi.service.ISearch;
import cn.poco.pepsi.util.DownloadImage;
import cn.poco.pepsi.util.UrlConnectionUtil;
import cn.poco.pepsi.util.UrlMatchUtil;
import cn.poco.pepsi.util.xmlpared.AreaXmlparse;
import cn.poco.pepsi.util.xmlpared.CircleXmlparse;
import cn.poco.pepsi.util.xmlpared.DishXmlparse;
import cn.poco.pepsi.util.xmlpared.HotKeyXmlparse;
import cn.poco.pepsi.util.xmlpared.ResDetailXmlparse;
import cn.poco.pepsi.util.xmlpared.RestaurantXmlparse;


public class SearchImpl implements ISearch {
	private Context context;
	 
	public SearchImpl(Context context){
		this.context=context;
	}
	
	@Override
	public List<Circle> getHotCircle(String citycode, Integer num) throws Exception {
		String url = HOT_PATH.replace("#a", String.valueOf(citycode)).replace("#b", String.valueOf(num));
		url=UrlMatchUtil.matchUrl(url);
		String url1=new DownloadImage(context).download(url);
		InputStream inStream = new FileInputStream(new File(url1));
		Log.i(TAG, url);
		return CircleXmlparse.getXml(inStream);
	}

	@Override
	public ArrayList<Area> getArea(String citycode, Context context) throws Exception {
		String url = null;
		ArrayList<Area> list = null;
		InputStream instream = null;
		/*
		CacheDbService dishservice = new CacheDbService(context);
		if (dishservice.checkArea(citycode) == 0) {
			url = AREA_PATH.replace("#a", citycode);
			instream = UrlConnectionUtil.getRequest(url);
			list = AreaXmlparse.getXml(instream);
			if (dishservice.checkArea(citycode) == 0) {
				dishservice.saveArea(citycode, list);
			}
		} else {
			list = (ArrayList<Area>) dishservice.getArea(citycode);
		}
		*/
		url = AREA_PATH.replace("#a", citycode);
		url=UrlMatchUtil.matchUrl(url);
		String url1=new DownloadImage(context).download(url);
		instream = new FileInputStream(new File(url1));
		list = AreaXmlparse.getXml(instream);
		return list;
	}

	@Override
	public ArrayList<Area> getSubArea(String citycode, String lid) throws Exception {
		String url = null;
		InputStream instream = null;
		if (citycode != null && !citycode.equals("") && lid != null && !lid.equals("")) {
			url = SUB_AREA_PATH.replace("#a", citycode).replace("#b", lid);
			url=UrlMatchUtil.matchUrl(url);
			instream = UrlConnectionUtil.getRequest(url);
		}
		return AreaXmlparse.getXml(instream);
	}

	@Override
	public ArrayList<Dish> getDishs(final String citycode, String type, final Context context) throws Exception {
		String reqUrl = null;
		InputStream instream = null;
		ArrayList<Dish> dishs = null;
		/*final CacheDbService dishservice = new CacheDbService(context);
		if (citycode != "" && citycode != null) {
			if (dishservice.checkDish(citycode) == 0) {
				reqUrl = DISH_PATH.replace("#a", citycode).replace("#b", type);
				instream = UrlConnectionUtil.getRequest(reqUrl);
				dishs = DishXmlparse.getXml(instream, citycode);
				Log.i(TAG, reqUrl);
				dishservice.saveDishs((ArrayList<Dish>) dishs);
			}else {
				dishs=dishservice.getDishs(citycode);
			}
		}*/
		String url = DISH_PATH.replace("#a", citycode).replace("#b", type);
		url=UrlMatchUtil.matchUrl(url);
		String url1=new DownloadImage(context).download(url);
		instream = new FileInputStream(new File(url1));
		dishs = DishXmlparse.getXml(instream, citycode);
		return dishs;
	}

	@Override
	public ArrayList<Restaurant> getFilterRestaurant(String key, String lid, String dish, String cd, int s, String l,
			String dis, String vouch, String locationid, String dishid,boolean ispepsi,String msg) throws Exception {
		InputStream inStream = null;
		StringBuffer sb = new StringBuffer();
		StringBuffer sb_pepsi=new StringBuffer();
		StringBuffer sb_pop_res=new StringBuffer();
		String url="";
		sb.append(BASE_SEARCH_PATH);
		sb_pepsi.append(PEPSI_RES_PATH);
		sb_pop_res.append(POP_RES_PATH);
		if (key != null) {
			sb.append("?key=").append(URLEncoder.encode(key, "GBK"));
		} else {
			sb.append("?key=");
		}
		if (lid != null && !lid.equals("")) {
			sb.append("&lid=").append(lid);

		}
		if (dish != null) {
			sb.append("&dish=").append(dish);
		}
		if (cd != null) {
			sb.append("&cd=").append(cd);
			sb_pop_res.append("?cd=").append(cd);
		}
		if (String.valueOf(s) != null) {
			sb.append("&s=").append(s);
			sb_pepsi.append("?s=").append(s);
			sb_pop_res.append("&s=").append(s);
		}
		if (l != null) {
			sb.append("&l=").append(l);
			sb_pepsi.append("&l=").append(l);
			sb_pop_res.append("&l=").append(l);
		}
		if (dis != null) {
			switch (Integer.parseInt(dis)) {
			case 0:
				sb.append("&produce=0");
				break;
			case 1:
				sb.append("&service=0");
				break;
			case 2:
				sb.append("&average=0");
				break;
			case 3:
				sb.append("&vouch=0");
				break;
			default:
				break;
			}
		}
		if (locationid != null) {
			sb.append("&locationid=").append(locationid);
		}
		if (dishid != null) {
			sb.append("&dishid=").append(dishid);
		}
		
		
		if(msg==null){
			if(!ispepsi){
				System.out.println("获取普通餐厅");
				url=UrlMatchUtil.matchUrl(sb.toString());
			}else{
				System.out.println("获取百事餐厅");
				url=UrlMatchUtil.matchUrl(sb_pepsi.toString());
			}
		}else{ 
			
			if(msg.equals("POP_RES")){
			System.out.println("获取推荐餐厅");
			url=UrlMatchUtil.matchUrl(sb_pop_res.toString());
		     }
			
			
		}
		Log.i(TAG, url);
		inStream = UrlConnectionUtil.getRequest(url);
		return RestaurantXmlparse.getXml(inStream);
	}

	
	//得到餐厅详情
	@Override
	public ResDetail getResDetail(String resid) throws Exception {
		String reqUrl = null;
		InputStream instream = null;
		if (resid != "" && resid != null) {
			reqUrl = RESTAURANT_DEATAIL_PATH.replace("#o", resid);
			reqUrl=UrlMatchUtil.matchUrl(reqUrl);
			String url1=new DownloadImage(context).download(reqUrl);
			instream=new FileInputStream(new File(url1));
		}
		return ResDetailXmlparse.getXml(instream);
	}

	
	//热门搜索词
	@Override
	public  ArrayList<HotSearchKey>  getHotkey() throws Exception {
		

		String reqUrl = null;
		InputStream instream = null;
		reqUrl = HOT_KEY_PATH;
		//reqUrl=UrlMatchUtil.matchUrl(reqUrl);
		String url1=new DownloadImage(context).download(reqUrl);
		instream=new FileInputStream(new File(url1));
		ArrayList<HotSearchKey> list=HotKeyXmlparse.getXml(instream);
		int n=list.size();
		return list;
	}

	
	//获取签到最热餐厅
	
	@Override
	public List<Restaurant> getOnHereHotRes(String from, String to, String lid,
			String l) throws Exception {
	List<Restaurant> list=null; 
	String url=ONHERE_HOT_RES_PATH.replace("#f", from).replace("#t", to).replace("#a", lid).replace("#l", l);
	
	InputStream inStream = UrlConnectionUtil.getRequest(url);
	list=RestaurantXmlparse.getXml(inStream);
		return list;
	}

}
